"""
EmailQA_SOP Domain Model
"""

from dataclasses import dataclass
from datetime import datetime
from typing import Optional

@dataclass
class EmailQA_SOP:
    """EmailQA_SOP association entity"""
    id: Optional[int] = None
    emailqa_id: Optional[int] = None
    sop_id: Optional[int] = None
    created_at: Optional[datetime] = None
    created_by: Optional[str] = None
    
    def __post_init__(self):
        """Validate EmailQA_SOP data"""
        if self.emailqa_id is not None and self.emailqa_id <= 0:
            raise ValueError("emailqa_id must be a positive integer")
        
        if self.sop_id is not None and self.sop_id <= 0:
            raise ValueError("sop_id must be a positive integer")
        
        if self.created_by is not None and not self.created_by.strip():
            raise ValueError("created_by cannot be empty")
    
    def to_dict(self) -> dict:
        """Convert EmailQA_SOP to dictionary"""
        return {
            'id': self.id,
            'emailqa_id': self.emailqa_id,
            'sop_id': self.sop_id,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'created_by': self.created_by
        }
    
    @classmethod
    def from_dict(cls, data: dict) -> 'EmailQA_SOP':
        """Create EmailQA_SOP from dictionary"""
        return cls(
            id=data.get('id'),
            emailqa_id=data.get('emailqa_id'),
            sop_id=data.get('sop_id'),
            created_at=datetime.fromisoformat(data['created_at']) if data.get('created_at') else None,
            created_by=data.get('created_by')
        )